package apps.web.service;

import java.util.ArrayList;
import java.util.List;

import webframe.common.file.FileTransferHelper;
import apps.web.model.UploadFile;

public class FileBizService extends MyBaseService {
	public void deleteImgFiles(List<Long> deleteFileId, String imgPath, String previewPath) {
		List<UploadFile> list = new ArrayList<UploadFile>();
		for (Long id : deleteFileId) {
			list.add(getUploadFileDao().get(id));
		}
		for (UploadFile img : list) {
			getUploadFileDao().delete(img);
			if (imgPath != null) {
				FileTransferHelper.deleteFile(img.getFileName(), imgPath);
			}
			if (previewPath != null) {
				FileTransferHelper.deleteFile(img.getPreviewImgName(), previewPath);
			}
		}
	}

	public void saveImgFiles(List<UploadFile> files) {
		for (UploadFile file : files) {
			getUploadFileDao().save(file);
		}
	}

	public void updateImgFiles(List<UploadFile> files) {
		for (UploadFile file : files) {
			getUploadFileDao().update(file);
		}
	}

	public List<UploadFile> findImgListBytempId(String tempId) {
		return getUploadFileDao().findByHql("from UploadFile t where t.tempId=? order by t.id",
				new Object[] { tempId });
	}

	public List<UploadFile> findImgListById(Long id) {
		return getUploadFileDao().findByHql("from UploadFile t where t.applyId=? order by t.id",
				new Object[] { id });
	}
}
